-- stone_skin
-- create by panyl
-- 石肤术

return {
    apply = function(source, target, skillId, round, para, extra)
        local effectRound = 0;
        local floor = DungeonM.currentLayer();
        extra = extra or {};
        local lastRound = extra.lastRound;

        if type(lastRound) == "number" then
            effectRound = round + lastRound + 1;
        elseif para[2] > 0 then
            -- 不包括本回合
            effectRound = round + para[2] + 1;
        end

        local prop = PropM.combine(source, "magic_last", skillId);
        effectRound = PropM.apply(prop, effectRound);
        prop = PropM.combine(source, "magic_last", "assist");
        effectRound = PropM.apply(prop, effectRound);

        -- 所有技能持续时间+
        prop = PropM.combine(source, "magic_last", "all");
        effectRound = PropM.apply(prop, effectRound);

        -- 1. 威力
        local force = 1000;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, true);
        -- 最终威力
        local force = math.modf(force * (addon_rate + 1000) / 1000);

        -- 2. 魔力
        local magic = source:getMagic();

        -- 千分比
        local rate = math.modf((1000 * magic + (magic + 15) * (force - 1000)) / (magic + 15));
        rate = math.modf(para[1] * rate / force);

        -- local propId = PropM.getPropId("damage_by_floor");

        -- local arr = { propId, floor, rate, effectRound, };

        -- 增加命中动作
        SkillM.getSequence(target):hit(source, target, skillId);

        local condition = {
            ["reduce_damage"] = rate,
            ["floor"]         = floor,
            ["end_round"]     = effectRound,
        };

        -- 记录属性
        -- PropM.recordBuff(target, arr);

        -- 附带概率免疫伤害
        prop = PropM.combine(source, "skill_add_invincible", skillId);
        if prop[3] > 0 then
            condition["invincible"] = prop[3];
            -- propId = PropM.getPropId("probability_invincible");
            -- arr = { propId, 1, prop[3], effectRound, };
            -- -- 记录属性
            -- PropM.recordBuff(target, arr, true);
        end

        -- 赋状态
        CombatStatusM.applyStatus(target, "stone_skin", condition);

        -- 作用目标
        return { target, };
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2] or -1) };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        local floor = DungeonM.currentLayer();
        -- 1. 威力
        local base = 1000;
        -- 计算魔法效果加成
        local addon_rate = SkillM.calcMagicEffectAddon(source, skillId, false);
        -- 最终威力
        local force = math.modf(base * (addon_rate + 1000) / 1000);

        -- 2. 魔力
        local magic = source:getMagic();

        -- 百分比
        --local rateBase = math.modf(para[1] / 10 * math.modf((1000 * magic + (magic + 15) * (base - 1000)) / (magic + 15))/ base);
        local rateForce = math.modf(para[1] / 10 * math.modf((1000 * magic + (magic + 15) * (force - 1000)) / (magic + 15)) / force);
        --local addon = rateForce - rateBase;
        desc = string.gsub(desc, "{reduce_damage}", rateForce);

        local key = FormulaM.invoke("CALC_SKILL_APPEND_PROP", skillId);
        local prop = PropM.combine(source, key, skillId);
        local append = math.modf(PropM.apply(prop, 1) / 10);
        desc = string.gsub(desc, "{append}", append);

        -- 回合数的显示
        local baseRound = para[2];
        local round = para[2];
        -- 增加持续时间的效果
        prop = PropM.combine(source, "magic_last", skillId);
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "assist");
        round = PropM.apply(prop, round);
        prop = PropM.combine(source, "magic_last", "all");
        round = PropM.apply(prop, round);

        local addonRound = round - baseRound;
        if addonRound == 0 then
            desc = string.gsub(desc, "{round}", baseRound);
        else
            desc = string.gsub(desc, "{round}", baseRound .. "(+" .. addonRound .. ")");
        end

        return desc;
    end,
};
